package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.api.dto.BasicPaymentPoolDto;
import com.ruicar.afs.cloud.basic.common.condition.PaymentStatusCondition;
import com.ruicar.afs.cloud.basic.common.entity.BasicChannelInfo;
import com.ruicar.afs.cloud.basic.common.service.BasicChannelInfoService;
import com.ruicar.afs.cloud.basic.proceeds.pay.entity.BasicPaymentPool;
import com.ruicar.afs.cloud.basic.proceeds.pay.service.BasicPaymentPoolService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author: xudongdong
 * @date: 2020/7/15
 * @description: 合同付款池controller
 */
@Slf4j
@RestController
@RequestMapping("/basicPaymentPool")
@AllArgsConstructor
public class BasicPaymentPoolController {

    private final BasicPaymentPoolService basicPaymentPoolService;
    private final BasicChannelInfoService basicChannelInfoService;

    @ApiOperation(value = "更新状态")
    @PostMapping("/updateStatus")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> updateStatus(@RequestBody PaymentStatusCondition paymentStatus) {
        basicPaymentPoolService.update(Wrappers.<BasicPaymentPool>lambdaUpdate()
                .eq(BasicPaymentPool::getContractNo, paymentStatus.getContractNo())
                .set(BasicPaymentPool::getPaymentStatus, paymentStatus.getPaymentStatus()));
        return new IResponse().setData(true);
    }

    @ApiOperation(value = "根据合同号查询放款信息")
    @PostMapping("/getPaymentPool")
    public IResponse getPaymentPool(@RequestParam("contractNo") String contractNo) {
        BasicPaymentPool basicPaymentPool = basicPaymentPoolService.getOne(Wrappers.<BasicPaymentPool>lambdaQuery()
                .eq(BasicPaymentPool::getContractNo, contractNo));
        BasicChannelInfo basicChannelInfo = basicChannelInfoService.getOne(Wrappers.<BasicChannelInfo>lambdaQuery()
                .eq(BasicChannelInfo::getContractNo, contractNo));
        BasicPaymentPoolDto dto = new BasicPaymentPoolDto();
        BeanUtils.copyProperties(basicPaymentPool, dto);
        dto.setReceiptBankName(basicChannelInfo.getReceivingSubBranch());
        return new IResponse().setData(dto);
    }

}
